import {computed, ref} from 'vue'
import {defineStore} from 'pinia'

export const useCounterStore = defineStore('counter', () => {
    const count = ref(0)
    const doubleCount = computed(() => count.value * 2)

    function increment() {
        count.value++
    }

    return {count, doubleCount, increment}
})

export const CommonStore = defineStore('CommonStore', () => {
    const treeProps = {
        children: 'children',
        label: 'name',
    }
    const dataPage = ref({
        current: 1,
        size: 30,
        total: 1,
    })

    function setDataPage(page) {
        if (page.hasOwnProperty("current")) {
            dataPage.value.current = page.current
        }
        if (page.hasOwnProperty("size")) {
            dataPage.value.size = page.size
        }
        if (page.hasOwnProperty("total")) {
            dataPage.value.total = page.total
        }
    }

    const currentRoute = ref("/")

    function setCurrentRoute(path) {
        currentRoute.value = path
    }

    return {treeProps, dataPage, setDataPage, currentRoute, setCurrentRoute}
})

export const CurrentAccountStore = defineStore("CurrentAccountStore", () => {
    const CurrentAccount = ref({
        ukId: "",
        username: "",
    })

    function setCurrentAccount(currentAccount) {
        CurrentAccount.value = currentAccount
    }

    return {CurrentAccount, setCurrentAccount}
});

export const PageStore = defineStore('PageStore', () => {
    const routerPageStore = ref({})

    function setRouterPageStore(route, page) {
        if (page.hasOwnProperty("current")) {
            routerPageStore.value[route] = {
                current: page.current,
                size: page.size,
                total: page.total
            }
        }
    }

    return {routerPageStore, setRouterPageStore}
})


export const manageStore = defineStore("manageStore", () => {
    const status = ref([
        {
            code: "enable",
            name: "enable",
            value: "enable",
        },
        {
            code: "disable",
            name: "disable",
            value: "disable",
        }
    ])
    return {status}
});
